package team.se.tms.controller.label_lib;

import com.google.gson.Gson;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.servlet.ModelAndView;
import team.se.tms.annotation.CheckFormat;
import team.se.tms.annotation.Regex;
import team.se.tms.controller.BaseController;
import team.se.tms.dto.LabelLibDetail;
import team.se.tms.dto.Page;
import team.se.tms.exception.InsertException;
import team.se.tms.exception.UpdateException;
import team.se.tms.po.LabelInfo;
import team.se.tms.po.TeachCourseInfo;
import team.se.tms.service.LabelInfoService;
import team.se.tms.service.LabelLibInfoService;
import team.se.tms.service.TeachCourseInfoService;
import team.se.tms.util.ResultUtil;

import javax.servlet.http.HttpSession;
import java.sql.SQLException;
import java.util.List;

@Controller
@RequestMapping("label_lib")
public class LabelLibInfoController extends BaseController{
    @Autowired
    LabelInfoService labelInfoService;
    @Autowired
    LabelLibInfoService labelLibInfoService;
    @Autowired
    TeachCourseInfoService teachCourseInfoService;

    /**
     * 去标签库列表界面
     *
     * @return String 页面地址
     * @throws Exception 异常
     * @create on 2018/11/24 by johnny
     */
    @RequestMapping("list")
    public String toSearchLabelLibs() throws Exception{
        return "label_lib/label_lib_list";
    }
    @RequestMapping("view")
    public ModelAndView toViewLabelLibDetail(String labelLibId,HttpSession session) throws Exception{
        ModelAndView mv = this.getModelAndView();
        List<TeachCourseInfo> teachCourseInfoList =teachCourseInfoService.listTeachCourseInfosByLabelLibId(labelLibId);
        //从session中获取要修改的标签库的信息
        List<LabelLibDetail> labelLibDetails = (List<LabelLibDetail>)session.getAttribute("labelLibDetails");
        LabelLibDetail labelLibDetail = new LabelLibDetail();
        if(labelLibDetails!=null && labelLibDetails.size()>0){
            for(LabelLibDetail item:labelLibDetails){
                if((item.getLabelLibId().trim()).equals(labelLibId.trim())){
                    labelLibDetail = item;
                    break;
                }
            }
        }
        mv.addObject("labelLibDetail",labelLibDetail);
        mv.addObject("teachCourseInfoList",teachCourseInfoList);
        mv.setViewName("label_lib/label_lib_detail");
        return mv;
    }
    /**
     * 去添加标签库界面
     *
     * @return ModelAndView
     * @throws Exception 异常
     * @create on 2018/11/24 by johnny
     */
    @RequestMapping("toAdd")
    public ModelAndView toAddLabelLib() throws Exception{
        ModelAndView mv = this.getModelAndView();
        List<LabelInfo> labelInfoList = labelInfoService.listAllLabelInfos(null);
        mv.addObject("labelList",labelInfoList);
        mv.addObject("dotype","add");
        mv.setViewName("label_lib/modify_label_lib");
        return mv;
    }

    /**
     * 去修改标签库界面
     *
     * @return ModelAndView
     * @throws Exception 异常
     * @create on 2018/11/24 by johnny
     */
    @RequestMapping("toEdit")
    public ModelAndView toEditLabelLib(String labelLibId,HttpSession session) throws Exception{
        Gson gson = new Gson();
        ModelAndView mv = this.getModelAndView();
        //获取所有的标签
        List<LabelInfo> labelInfoList = labelInfoService.listAllLabelInfos(null);
        //从session中获取labelLibDetails，取到id 等于要修改的标签库的id
        List<LabelLibDetail> labelLibDetails = (List<LabelLibDetail>)session.getAttribute("labelLibDetails");
        LabelLibDetail labelLibDetail = new LabelLibDetail();
        if(labelLibDetails!=null && labelLibDetails.size()>0){
            for(LabelLibDetail item:labelLibDetails){
                if((item.getLabelLibId().trim()).equals(labelLibId.trim())){
                    labelLibDetail = item;
                    break;
                }
            }
        }

        //将所有标签返回给界面
        mv.addObject("labelList",labelInfoList);
        //获取作业中被使用的标签
        List<String> usingLabelIds = labelLibInfoService.listUsingLabelIds(labelLibDetail.getLabelLibId());
        //System.out.println("usingLabelIds.get(0)"+usingLabelIds.get(0));
        //将作业中被使用的标签返回给界面
        mv.addObject("usingLabelIds",gson.toJson(usingLabelIds));
        //将要编辑的标签库信息返回给界面
        mv.addObject("labelLibDetail",gson.toJson(labelLibDetail));
        //操作类型
        mv.addObject("dotype","edit");
        mv.setViewName("label_lib/modify_label_lib");
        return mv;
    }


    /**
     * 添加标签库
     *
     * @param labelLibDetail 标签库信息
     * @return Object 添加结果
     * @throws Exception 异常
     * @create on 2018/11/24 by johnny
     */
    @RequestMapping("add")
    @ResponseBody
    @CheckFormat({"labelLibId","labelName"})
    public Object addLabelLib(@RequestBody LabelLibDetail labelLibDetail) throws Exception{
        boolean result = labelLibInfoService.saveLabelLib(labelLibDetail);
        if(!result){
            throw new InsertException();
        }
        return ResultUtil.success();
    }

    /**
     * 分页查询标签库信息
     *
     * @param page 分页
     * @return String 标签库列表的json字符串
     * @throws Exception 异常
     * @create on 2018/11/24 by johnny
     */
    @RequestMapping("getList")
    @ResponseBody
    public String searchLabelLibs(Page page, HttpSession httpSession) throws Exception{
        List<LabelLibDetail> labelLibDetails = labelLibInfoService.listAllLabelLibDetails(page);
        httpSession.setAttribute("labelLibDetails",labelLibDetails);
        return page.getDataTabelJson(labelLibDetails);
    }

    /**
     * 修改标签库信息
     *
     * @param labelLibDetail 标签库信息
     * @return Object 修改结果
     * @throws Exception 异常
     * @create on 2018/11/24 by johnny
     */
    @RequestMapping("edit")
    @ResponseBody
    @CheckFormat({"labelName"})
    public Object editLabelLib(@RequestBody LabelLibDetail labelLibDetail,HttpSession session) throws Exception{
        try{
            //认为modifyLabelLib没有抛出异常则认为执行成功，因为modifyLabelLib需要开启事务，
            // 在modifyLabelLib 中 try容易吃掉异常，所以在这里try
            labelLibInfoService.modifyLabelLib(labelLibDetail);
        }catch (SQLException e){
            throw new  UpdateException();
        }
        return ResultUtil.success();
    }

    /**
     * 删除标签库信息
     *
     * @param labelLibId 标签库id
     * @return Object 删除结果
     * @throws Exception 异常
     * @create on 2018/11/24 by johnny
     */
    @RequestMapping("drop")
    @ResponseBody
    @CheckFormat
    public Object dropLabelLib(@Regex("^[A-Za-z0-9]{16}$") String labelLibId) throws Exception{
        labelLibInfoService.removeLabelLib(labelLibId);
        return ResultUtil.success();
    }


}

